import '../../../app_files.dart';

class ConfigValue {
  final String key;
  final String name;

  const ConfigValue({
    this.key,
    this.name,
  });

  // static fromJson(Map<String, dynamic> json) {
  //   return ConfigValue(
  //     key: CommonUtils.checkNullToString(json['Key']),
  //     name: CommonUtils.checkNullToString(json['Value']),
  //   );
  // }

  static fromJson(String json) {
    return ConfigValue(
      key: json,
      name: json,
    );
  }

  @override
  bool operator ==(other) {
    if (other.runtimeType != runtimeType) return false;
    final ConfigValue typedOther = other;
    return typedOther.key == key && typedOther.name == name;
  }

  @override
  int get hashCode => name.hashCode ^ key.hashCode;
}

class OrderConfigModel {
  final String configID;
  final String configName;
  final List<ConfigValue> configs;
  final List<ConfigValue> values;
  bool isNormal;
  final bool isRemind;
  final bool canNone;
  bool hasOther;
  TextEditingController textEditingController = TextEditingController();
  final List imageList;
  final String valueDesc;

  String get valueStr {
    if (valueDesc != null) return valueDesc;
    if (this.isNormal) return '正常';
    List<String> values = this.values.map((e) => e.name).toList();
    if (this.hasOther) values.add(this.textEditingController.text);
    return values.join('、');
  }

  bool get isNoneNormal {
    return values.length == 1 && values.first.name == '无' && canNone;
  }

  OrderConfigModel({
    this.configID,
    this.configName,
    this.configs,
    this.values,
    this.imageList,
    this.hasOther,
    this.valueDesc,
    this.isRemind = false,
    this.canNone = false,
  });

  static fromConfigJson(Map<String, dynamic> json) {
    List configValue = json['ConfigContent'];
    List<ConfigValue> configs =
        configValue.map<ConfigValue>((e) => ConfigValue.fromJson(e)).toList();
    return OrderConfigModel(
      configID: CommonUtils.checkNullToString(json['ConfigId']),
      configName: CommonUtils.checkNullToString(json['ConfigName']),
      isRemind: json['isRemind'] ?? false,
      canNone: json['canNone'] ?? false,
      configs: configs,
      values: List<ConfigValue>(),
      imageList: List(),
      hasOther: false,
    );
  }

  static fromJson(json) {
    return OrderConfigModel(
      configID: CommonUtils.checkNullToString(json['ConfigId']),
      configName: CommonUtils.checkNullToString(json['ConfigKey']),
      imageList: json['ImageList'],
      valueDesc: CommonUtils.checkNullToString(json['ConfigValue']),
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['ConfigId'] = this.configID;
    data['ConfigKey'] = this.configName;
    data['ConfigResult'] = 1;
    if (this.isNormal)
      data['ConfigValue'] = '正常';
    else {
      data['ImageList'] = this.imageList;
      data['ConfigValue'] = this.valueStr;
      data['ConfigResult'] = 0;
    }
    return data;
  }

  static String getResultStr(List<OrderConfigModel> items) {
    List<String> res = items.map<String>((e) {
      return '${e.configName}：${e.valueStr}';
    }).toList();
    return res.join('\r\n');
  }
}

class UserGasInfo {
  final String gasBuyTotal;
  final String gasUseTotal;
  final String gasRemainTotal;
  TextEditingController gasBuyTotalController = TextEditingController();
  TextEditingController gasUseTotalController = TextEditingController();
  TextEditingController gasRemainTotalController = TextEditingController();
  TextEditingController meterBaseVolumeController = TextEditingController();

  String get gasBuyTotalStr => gasBuyTotal ?? gasBuyTotalController.text;

  String get gasUseTotalStr => gasUseTotal ?? gasUseTotalController.text;

  String get gasRemainTotalStr =>
      gasRemainTotal ?? gasRemainTotalController.text;

  UserGasInfo({
    this.gasBuyTotal,
    this.gasUseTotal,
    this.gasRemainTotal,
  });

  static fromJson(json) {
    return UserGasInfo(
      gasBuyTotal: CommonUtils.checkNullToString(json['CheckOrderGasTotal']),
      gasUseTotal: CommonUtils.checkNullToString(json['CheckOrderUseGasTotal']),
      gasRemainTotal:
          CommonUtils.checkNullToString(json['CheckOrderLeaveGasTotal']),
    );
  }
}
